//
//  SKMapShowViewController.m
//  Gastrosoph
//
//  Created by 张明磊 on 12-10-17.
//  Copyright (c) 2012年 __MyCompanyName__. All rights reserved.
//

#import "SKMapShowViewController.h"
#import "SKHeaderFiles.h"
#import "SKMapAnnotation.h"
#import "SKCalloutMapAnnotation.h"
#import "SKCalloutMapAnnotationView.h"


#define SK_BUTTON_MAPSHOW_SEARCHAGAIN_CGRECT CGRectMake(10, 30, 260, 32)

#define SK_BUTTON_MAPSHOW_SHOWLOCATION_CGRECT CGRectMake(276, 30, 33, 32)


@interface SKMapShowViewController ()

@end

@implementation SKMapShowViewController

@synthesize calloutAnnotation = _calloutAnnotation;

@synthesize locationManager = _locationManager;

@synthesize selectedAnnotationView = _selectedAnnotationView;

@synthesize mapView_googleMap = _mapView_googleMap;


- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    [self install_view_map];
    
    [self install_viewButton_mapShow];
    
    SKMapAnnotation *customAnnotation = [[[SKMapAnnotation alloc] initWithLatitude:40.058220 andLongitude:116.406209] autorelease];
    
	[_mapView_googleMap addAnnotation:customAnnotation];
    
	// Do any additional setup after loading the view.
}

- (void)viewDidUnload
{
    [_locationManager stopUpdatingLocation];
    
    [super viewDidUnload];
    
    // Release any retained subviews of the main view.
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}


- (void)install_view_map
{
    self.locationManager = [[[CLLocationManager alloc] init] autorelease];
    
    [_locationManager startUpdatingLocation];
    
    _locationManager.delegate = self;
    
	_locationManager.desiredAccuracy = kCLLocationAccuracyBest;
    
	_locationManager.distanceFilter = 500;
    
    self.mapView_googleMap = [[[MKMapView alloc] initWithFrame:CGRectMake([UIScreen mainScreen].bounds.origin.x, [UIScreen mainScreen].bounds.origin.y - 20, [UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height - 20)] autorelease]; 
    
    [_mapView_googleMap setDelegate:self];
    
    MKCoordinateSpan theSpan;
    
    theSpan.latitudeDelta = 0.02;
	
    theSpan.longitudeDelta = 0.02;	
    
    MKCoordinateRegion theRegion; 

    theRegion.center = [[self.locationManager location] coordinate];
  
    theRegion.span = theSpan;
    
    [_mapView_googleMap setRegion:theRegion];
    
    [_mapView_googleMap setMapType:MKMapTypeStandard];
    
    _mapView_googleMap.showsUserLocation = YES;
    
    [self.view addSubview:_mapView_googleMap];
    
}


- (void)dealloc
{
    [_locationManager stopUpdatingLocation];
    
    [_locationManager release], _locationManager = nil;
    
    [_selectedAnnotationView release], _selectedAnnotationView = nil;
    
    [_mapView_googleMap release], _mapView_googleMap = nil;
    
    [super dealloc];
}


- (void)install_viewButton_mapShow
{
    UIButton *button_searchAgain = [UIButton buttonWithType:UIButtonTypeCustom];
    
    [button_searchAgain setFrame:SK_BUTTON_MAPSHOW_SEARCHAGAIN_CGRECT];
    
    [button_searchAgain setBackgroundImage:[UIImage imageNamed:@"button_mapShow_searchAgain.png"] forState:UIControlStateNormal];
    
    [button_searchAgain.titleLabel setFont:[UIFont fontWithName:SK_TEXTFONT size:17.0]];
    
    [button_searchAgain setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    
    [button_searchAgain setTitle:@"在地图范围内再次搜索" forState:UIControlStateNormal];
    
    [button_searchAgain setBackgroundImage:[UIImage imageNamed:@"button_mapShow_searchAgain_pressed.png"] forState:UIControlStateHighlighted];
    
    [self.view addSubview:button_searchAgain];
    
    UIButton *button_showLocation = [UIButton buttonWithType:UIButtonTypeCustom];
    
    [button_showLocation setFrame:SK_BUTTON_MAPSHOW_SHOWLOCATION_CGRECT];
    
    [button_showLocation setBackgroundImage:[UIImage imageNamed:@"button_showLocation.png"] forState:UIControlStateNormal];
    
    [button_showLocation setBackgroundImage:[UIImage imageNamed:@"button_showLocation_pressed.png"] forState:UIControlStateHighlighted];
    
    [button_showLocation addTarget:self action:@selector(didClickButton_showLocation) forControlEvents:UIControlEventTouchUpInside];
    
    [self.view addSubview:button_showLocation];
}


- (void)didClickButton_showLocation
{
    self.mapView_googleMap.showsUserLocation = NO;
    
    self.mapView_googleMap.showsUserLocation = YES;
    
    MKCoordinateSpan theSpan;
    
    theSpan.latitudeDelta = 0.02;
	
    theSpan.longitudeDelta = 0.02;	
    
    MKCoordinateRegion theRegion; 
    
    theRegion.center = [[self.locationManager location] coordinate];
    
    theRegion.span = theSpan;
    
    [_mapView_googleMap setRegion:theRegion];
}


#pragma mark - 
#pragma mark MKMapViewDelegate

- (void)mapView:(MKMapView *)mapView didSelectAnnotationView:(MKAnnotationView *)view
{
		if (self.calloutAnnotation == nil)
        {
			self.calloutAnnotation = [[SKCalloutMapAnnotation alloc] initWithLatitude:view.annotation.coordinate.latitude
																	   andLongitude:view.annotation.coordinate.longitude];
		}
    
        else
        {
			self.calloutAnnotation.latitude = view.annotation.coordinate.latitude;
            
			self.calloutAnnotation.longitude = view.annotation.coordinate.longitude;
		}
    
		[_mapView_googleMap addAnnotation:self.calloutAnnotation];
        
		self.selectedAnnotationView = view;
}


- (void)mapView:(MKMapView *)mapView didDeselectAnnotationView:(MKAnnotationView *)view 
{
		[_mapView_googleMap removeAnnotation: self.calloutAnnotation];
}



- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>)annotation
{
    MKPinAnnotationView *pinView = nil;
    
    if(annotation != _mapView_googleMap.userLocation)
    {
        if(annotation == _calloutAnnotation)
        {
            SKCalloutMapAnnotationView *calloutMapAnnotationView = (SKCalloutMapAnnotationView *)[self.mapView_googleMap dequeueReusableAnnotationViewWithIdentifier:@"SKCalloutAnnotation"];
            
            if (!calloutMapAnnotationView)
            {
                
                calloutMapAnnotationView = [[[SKCalloutMapAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:@"SKCalloutAnnotation"] autorelease];
                
                calloutMapAnnotationView.contentHeight = 126.0f;

                UIImage *asynchronyLogo = [UIImage imageNamed:@"demo.png"];
                
                UIImageView *asynchronyLogoView = [[[UIImageView alloc] initWithImage:asynchronyLogo] autorelease];
                
                asynchronyLogoView.frame = CGRectMake(5, 2, asynchronyLogoView.frame.size.width, asynchronyLogoView.frame.size.height);
                
        
                [calloutMapAnnotationView.contentView addSubview:asynchronyLogoView];
                
                UILabel *label_foodName = [[UILabel alloc]initWithFrame:CGRectMake(137, 0, 160, 40)];
                
                [label_foodName setTextColor:[UIColor whiteColor]];
                
                [label_foodName setBackgroundColor:[UIColor clearColor]];
                
                [label_foodName setFont:[UIFont fontWithName:SK_TEXTFONT size:18.0]];
                
                [label_foodName setText:@"意大利式风味酱肉"];
                
                [calloutMapAnnotationView.contentView addSubview:label_foodName];
                
                [label_foodName release];
                
                UILabel *label_restaurantName = [[UILabel alloc]initWithFrame:CGRectMake(137, 23, 160, 30)];
                
                [label_restaurantName setTextColor:[UIColor orangeColor]];
                
                [label_restaurantName setBackgroundColor:[UIColor clearColor]];
                
                [label_restaurantName setFont:[UIFont fontWithName:SK_TEXTFONT size:16.0]];
                
                [label_restaurantName setText:@"来自  彼得潘意式餐厅"];
                
                [calloutMapAnnotationView.contentView addSubview:label_restaurantName];
                
                [label_restaurantName release];
                
                UILabel *label_restaurantLocation = [[UILabel alloc]initWithFrame:CGRectMake(137, 53, 160, 30)];
                
                [label_restaurantLocation setTextColor:[UIColor orangeColor]];
                
                [label_restaurantLocation setBackgroundColor:[UIColor clearColor]];
                
                [label_restaurantLocation setFont:[UIFont fontWithName:SK_TEXTFONT size:12.0]];
                
                [label_restaurantLocation setText:@"北京市海淀区万泉河路39号"];
                
                [calloutMapAnnotationView.contentView addSubview:label_restaurantLocation];
                
                [label_restaurantLocation release];
                
                UILabel *label_distance = [[UILabel alloc]initWithFrame:CGRectMake(260, 100, 50, 30)];
                
                [label_distance setTextColor:[UIColor orangeColor]];
                
                [label_distance setBackgroundColor:[UIColor clearColor]];
                
                [label_distance setFont:[UIFont fontWithName:SK_TEXTFONT size:12.0]];
                
                [label_distance setText:@"1.3km"];
                
                [calloutMapAnnotationView.contentView addSubview:label_distance];
                
                [label_distance release];
                
                UIImageView *image_location = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"mapShowView_location.png"]];
                
                [image_location setFrame:CGRectMake(242, 107, 11, 16)];
                
                [calloutMapAnnotationView.contentView addSubview:image_location];
                
                [image_location release];
  
            }
            
            calloutMapAnnotationView.parentAnnotationView = self.selectedAnnotationView;
            
            calloutMapAnnotationView.mapView = self.mapView_googleMap;
            
            return calloutMapAnnotationView;
        }
        
        else
        {
            static NSString *SKCustomAnnotationIdentifier = @"SKCustomAnnotationIdentifier";
            
            pinView = (MKPinAnnotationView *)[mapView dequeueReusableAnnotationViewWithIdentifier:SKCustomAnnotationIdentifier];
            
            MKPinAnnotationView *annotationView = [[[MKPinAnnotationView alloc] initWithAnnotation:annotation
                                                                                   reuseIdentifier:SKCustomAnnotationIdentifier] autorelease];
    
            annotationView.canShowCallout = NO;
            
            UIImage *flagImage = [UIImage imageNamed:@"ping.png"];
            
            CGRect resizeRect;
            
            resizeRect.size = flagImage.size;
            
            CGSize maxSize = CGRectInset(self.view.bounds,
                                         10.0f,
                                         10.0f).size;
            if (resizeRect.size.width > maxSize.width)
                resizeRect.size = CGSizeMake(maxSize.width, resizeRect.size.height / resizeRect.size.width * maxSize    .width);
            if (resizeRect.size.height > maxSize.height)
                resizeRect.size = CGSizeMake(resizeRect.size.width / resizeRect.size.height * maxSize.height, maxSize.height);
            if (resizeRect.size.width > maxSize.width)
                resizeRect.size = CGSizeMake(maxSize.width, resizeRect.size.height / resizeRect.size.width * maxSize.width);
            if (resizeRect.size.height > maxSize.height)
                resizeRect.size = CGSizeMake(resizeRect.size.width / resizeRect.size.height * maxSize.height, maxSize.height);
            resizeRect.origin = (CGPoint){0.0f, 0.0f};
            UIGraphicsBeginImageContext(resizeRect.size);
            [flagImage drawInRect:resizeRect];
            UIImage *resizedImage = UIGraphicsGetImageFromCurrentImageContext();
            UIGraphicsEndImageContext();
    
            annotationView.image = resizedImage;
            
            return annotationView;
        }
    }
    
    else
    {
        return nil;
    }
}


@end
